package com.ztom.v2;

/**
 * @author ZhangTao
 */
public class Code30SwapNodesInPairs {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            ListNode second = cur.next;
            ListNode third = second.next;

            pre.next = second;
            second.next = cur;
            cur.next = third;

            pre = cur;
            cur = third;
        }
        return dummy.next;
    }
}
